/**
 * 
 */
package com.sandberg.shopper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.sandberg.shopper.core.Calculator;
import com.sandberg.shopper.core.HelpScreen;
import com.sandberg.shopper.core.Utility;

/**
 * @author Jesper
 *
 */
public class ShopperPercentActivity extends Activity {
	private float valueNormal;
	private float valueSale;
	private float valuePercent;

	private final int DIALOG_WARNING = 1;
	private final int DIALOG_HELP = 2;

	public void onCalculate(View target) {
		if (validateInput() == false) {
			showDialog(DIALOG_WARNING);
		} else {
			if (valueNormal == 0) {
				valueNormal = Calculator.calcNormalPrice(valueSale, valuePercent);
				final EditText edit = (EditText) findViewById(R.id.editNormalPrice);
				edit.setText(Utility.round2digits(valueNormal));
			} else if (valueSale == 0) {
				valueSale = Calculator.calcSalesPrice(valueNormal, valuePercent);
				final EditText edit = (EditText) findViewById(R.id.EditSalesPrice);
				edit.setText(Utility.round2digits(valueSale));
			} else if (valuePercent == 0) {
				valuePercent = Calculator.calcPercent(valueNormal, valueSale);
				final EditText edit = (EditText) findViewById(R.id.EditSavePercent);
				edit.setText(Utility.round2digits(valuePercent));
			}
		}
	}
	public void onDeleteNormal(View target) {
		final EditText deleteNormalEdit = (EditText) findViewById(R.id.editNormalPrice);
		deleteNormalEdit.setText("");
	}

	public void onDeleteSale(View target) {
		final EditText deleteSaleEdit = (EditText) findViewById(R.id.EditSalesPrice);
		deleteSaleEdit.setText("");
	}

	public void onDeletePercent(View target) {
		final EditText deleteSaveEdit = (EditText) findViewById(R.id.EditSavePercent);
		deleteSaveEdit.setText("");
	}

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.percent);

        // Restore preferences
        SharedPreferences settings = getSharedPreferences(Property.SHARED_PREFS_NAME, 0);
        if (settings.getBoolean(Property.SHARED_STORE_FIELDS, true)) {
        	getStoredValues(settings);
        }
    }

    private void getStoredValues(SharedPreferences settings) {
        // Get normal value
        final EditText normal = (EditText) findViewById(R.id.editNormalPrice);
        String normalField = settings.getString(Property.SHARED_PERCENT_NORMALPRICE, "");
        normal.setText(normalField);
        // Get sale value
        final EditText sale = (EditText) findViewById(R.id.EditSalesPrice);
        String saleField = settings.getString(Property.SHARED_PERCENT_SALEPRICE, "");
        sale.setText(saleField);
        // Get percent value
        final EditText percent = (EditText) findViewById(R.id.EditSavePercent);
        String percentField = settings.getString(Property.SHARED_PERCENT_PERCENT, "");
        percent.setText(percentField);
    }

	private boolean validateInput() {
		String inputValue;
		int validFields = 0;
		
		/* Check normal */
		final EditText deleteNormalEdit = (EditText) findViewById(R.id.editNormalPrice);
		inputValue = deleteNormalEdit.getText().toString();
		if (inputValue.length() > 0) {
			valueNormal = Float.valueOf(inputValue);
			if (valueNormal != 0) {
				validFields++;
			}
		} else {
			valueNormal = 0;
		}

		/* Check sale */
		final EditText deleteSaleEdit = (EditText) findViewById(R.id.EditSalesPrice);
		inputValue = deleteSaleEdit.getText().toString();
		if (inputValue.length() > 0) {
			valueSale = Float.valueOf(inputValue);
			if (valueSale != 0) {
				validFields++;
			}
		} else {
			valueSale = 0;
		}

		/* Check save */
		final EditText deleteSaveEdit = (EditText) findViewById(R.id.EditSavePercent);
		inputValue = deleteSaveEdit.getText().toString();
		if (inputValue.length() > 0) {
			valuePercent = Float.valueOf(inputValue);
			if (valuePercent != 0) {
				validFields++;
			}
		} else {
			valuePercent = 0;
		}
		
		if (validFields == 2) {
			return true;
		} else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateDialog(int)
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
	    Dialog dialog = null;
	    switch(id) {
	    	case DIALOG_WARNING:
	    		dialog = createWarning();
	    		break;
	    	case DIALOG_HELP:
	    		dialog = HelpScreen.createHelpScreen(this, R.string.help_percent);
	    		break;
	    }
	    return dialog;
	}
	
	private AlertDialog createWarning() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getResources().getString(R.string.percent_warning));
		builder.setCancelable(false);
		builder.setPositiveButton(getResources().getString(R.string.warning_button), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
					}
				});
		return builder.create();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		menu.add(getResources().getString(R.string.option_menu_options))
			.setIcon(android.R.drawable.ic_menu_preferences)
			.setIntent(new Intent(this, ShopperSettingsActivity.class));

		menu.add(getResources().getString(R.string.option_menu_about))
			.setIcon(android.R.drawable.ic_menu_info_details)
			.setIntent(new Intent(this, ShopperAboutActivity.class));

		menu.add(getResources().getString(R.string.option_menu_help))
			.setIcon(android.R.drawable.ic_menu_help);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		String title = (String) item.getTitle();
		if (title.compareTo(getResources().getString(R.string.option_menu_help)) == 0) {
			showDialog(DIALOG_HELP);
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
	 */
	@Override
	protected void onPause() {
    	// We need an Editor object to make preference changes.
    	SharedPreferences settings = getSharedPreferences(Property.SHARED_PREFS_NAME, 0);
    	SharedPreferences.Editor editor = settings.edit();
		
        // Set normal value
        final EditText normal = (EditText) findViewById(R.id.editNormalPrice);
        String normalField = normal.getText().toString();
    	editor.putString(Property.SHARED_PERCENT_NORMALPRICE, normalField);

        // Get sales value
        final EditText sale = (EditText) findViewById(R.id.EditSalesPrice);
        String saleField = sale.getText().toString();
    	editor.putString(Property.SHARED_PERCENT_SALEPRICE, saleField);

        // Get percent value
        final EditText percent = (EditText) findViewById(R.id.EditSavePercent);
        String percentField = percent.getText().toString();
    	editor.putString(Property.SHARED_PERCENT_PERCENT, percentField);

    	editor.commit();

    	super.onPause();
	}
}
